﻿using System.Collections.Generic;
using System.Data;
using SSAT.Commands;
using SSAT.UserFriendly;
using SSAT.Utils;
using SSAT.Utils.Freezer;

namespace SSAT.Forms
{
    public partial class MainForm
    {
        private Dictionary<ConnectionState, string> _connectionStates;
        private ControlFreezer _connectionNullGroup;
        private ControlFreezer _runningAtsDriverGroup;

        private ExitApplicationCommand ExitApplicationCommand;
        private ConnectToServerCommand ConnectToServerCommand;
        private CloseConnectionCommand CloseConnectionCommand;
        private ClearConsoleCommand ClearConsoleCommand;
        private ReconnectCommand ReconnectCommand;
        private AuthentificationTroubleshooterRunCommand AuthentificationTroubleshooterRunCommand;
        private PrintReportAgainCommand PrintReportAgainCommand;
        private SaveReportCommand SaveReportCommand;
        private MonitorCommand MonitorCommand;

        private void Initialize()
        {
            InitializeConnectionStatesInfos();
            SetupAppConsole();
            
            RegisterCommands();
            InitializeControlsFreezers();
            UpdateState();
            WriteGreeting();
        }

        private void InitializeControlsFreezers()
        {
            _connectionNullGroup = new ControlFreezer();
            _connectionNullGroup.AddControl(ReconnectCommand);
            _connectionNullGroup.AddControl(CloseConnectionCommand);

            _runningAtsDriverGroup = new ControlFreezer();
            _runningAtsDriverGroup.AddControl(ConnectToServerCommand);
            _runningAtsDriverGroup.AddControl(CloseConnectionCommand);
            _runningAtsDriverGroup.AddControl(ReconnectCommand);
            _runningAtsDriverGroup.AddControl(AuthentificationTroubleshooterRunCommand);
            _runningAtsDriverGroup.AddControl(MonitorCommand);
        }

        private void SetupAppConsole()
        {
            AppConsole.ConsoleField = ucConsole;
        }

        private void RegisterCommands()
        {
            ExitApplicationCommand = new ExitApplicationCommand();
            UIHelper.RegisterToolStripMenuClickHandler(exitToolStripMenuItem, ExitApplicationCommand);

            ConnectToServerCommand = new ConnectToServerCommand(ShowConnectToServerDialog);
            UIHelper.RegisterToolStripMenuClickHandler(connectToolStripMenuItem, ConnectToServerCommand);
            UIHelper.RegisterToolStripMenuClickHandler(tsbConnect, ConnectToServerCommand);

            CloseConnectionCommand = new CloseConnectionCommand(CloseCurrentConnection);
            UIHelper.RegisterToolStripMenuClickHandler(closeConnectionToolStripMenuItem, CloseConnectionCommand);
            UIHelper.RegisterToolStripMenuClickHandler(tsbCloseConnection, CloseConnectionCommand);

            ClearConsoleCommand = new ClearConsoleCommand(ucConsole);
            UIHelper.RegisterToolStripMenuClickHandler(clearConsoleToolStripMenuItem, ClearConsoleCommand);
            UIHelper.RegisterToolStripMenuClickHandler(clearConsoleWindowToolStripMenuItem, ClearConsoleCommand);

            ReconnectCommand = new ReconnectCommand(this);
            UIHelper.RegisterToolStripMenuClickHandler(reconnectToolStripMenuItem, ReconnectCommand);

            AuthentificationTroubleshooterRunCommand = new AuthentificationTroubleshooterRunCommand(this);
            UIHelper.RegisterToolStripMenuClickHandler(troubleshooterToolStripMenuItem, AuthentificationTroubleshooterRunCommand);
            UIHelper.RegisterToolStripMenuClickHandler(tsbTroubleshooter, AuthentificationTroubleshooterRunCommand);

            PrintReportAgainCommand = new PrintReportAgainCommand(this);
            UIHelper.RegisterToolStripMenuClickHandler(printReportAgainToolStripMenuItem, PrintReportAgainCommand);

            SaveReportCommand = new SaveReportCommand(this);
            UIHelper.RegisterToolStripMenuClickHandler(saveReportToolStripMenuItem, SaveReportCommand);
            UIHelper.RegisterToolStripMenuClickHandler(tsbSaveReport, SaveReportCommand);

            MonitorCommand = new MonitorCommand(this);
            UIHelper.RegisterToolStripMenuClickHandler(monitorToolStripMenuItem, MonitorCommand);
            UIHelper.RegisterToolStripMenuClickHandler(tsbMonitor, MonitorCommand);
        }

        private void WriteGreeting()
        {
            ucConsole.WriteGreeting(Greeting.Next());
            ucConsole.WriteGreeting(Constants.UI.ConsoleGreetingsSeparator);
        }

        private void InitializeConnectionStatesInfos()
        {
            /* As far as current version of SqlConnection (and OleDbConnection) uses
             * only two members of enum ConnectionState we are adding only those two.
             */
            _connectionStates = new Dictionary<ConnectionState, string>()
            {
                { ConnectionState.Open, Resources.ConnectionStateOpen },
                { ConnectionState.Closed, Resources.ConnectionStateClose },
            };
        }
    }
}
